#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2017/11/13 14:42
# @Author  : Yunhao.Cao
# @File    : downloader.py
from __future__ import absolute_import, unicode_literals
from logger import Logger
import requests

__author__ = 'Yunhao.Cao'

__ALL__ = ["Downloader", ]


class Downloader:
    """
    下载器
    """

    def __init__(self):
        pass

    @staticmethod
    def get(url, **kwargs):
        """
        get请求
        :param url: 请求url
        :param kwargs:
        :return: 其他参数
        """
        kwargs = Downloader._configure(**kwargs)
        Logger.info("getting...(url={})".format(url))
        response = requests.get(url, **kwargs)
        Logger.info("get finished. (url={})".format(url))

        return response.text

    @staticmethod
    def post(url, data=None, **kwargs):
        """
        http post 请求
        :param url: 请求url
        :param data: post请求发送的data, 表单, 文件等
        :param kwargs: 其他参数
        :return:
        """
        kwargs = Downloader._configure(**kwargs)
        response = requests.post(url, data=data, **kwargs)

        return response.text

    @staticmethod
    def _configure(**kwargs):
        """
        配置request请求的参数
        - 包括:
            - header
            - proxies(dict) 或 proxy('ip:port')
        :param kwargs:
        :return: 设置后的 kwargs 参数
        """
        # 设置headers
        headers = {
            'User-Agent': "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.89 Safari/537.36"
        }
        # proxies
        proxy_ip = None
        if "proxy_ip" in kwargs:
            proxy_ip = kwargs.get("proxy_ip")
            del kwargs["proxy_ip"]

        proxies = {"http": "http://{}".format(proxy_ip), } if proxy_ip else None

        if "header" not in kwargs:
            kwargs["headers"] = headers
        if "proxies" not in kwargs:
            kwargs["proxies"] = proxies

        return kwargs


if __name__ == '__main__':
    text = Downloader.get("https://www.baidu.com")

    print(text.encode("utf-8"))
